<?php

namespace App\Http\Controllers;

use App\Http\Requests\AddTrainRequest;
use App\MakeTable\Table;
use App\Mentor;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use App\Train;
use Illuminate\Http\Request;
use Gate;
class TrainController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $pagename = '培训列表';
        $modelurl = 'trains';
        $tablecate = ['tablecate'=>'培训'];
        $headname = ['名称','地点','开始时间','报名人数','价格','电话','封面','简介','宣传标语','状态','承办俱乐部','负责人','操作'];
        $tablehead = ['name','address','activity_time','part_count','price','contact_tele','cover_image','intro','attention','status','club','mentor','id'];
        $traintable = new Table($tablehead,$page = 8);
        if(Gate::allows('club_Admin_train')){
            $model = $traintable->viewdatawithuser(Train::class);
            $models = $traintable->viewpagedatawithuser(Train::class);
        }
        else{
            $model = $traintable->viewdata(Train::class);
            $models = $traintable->viewpagedata(Train::class);
        }

        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (Gate::denies('club_Admin_train')) {
            return redirect('admin/trains');
        }
        //新增培训
        $modelurl = 'trains';
        $pagename = '新增培训';
        $formhead = ['name','address','activity_time','price','contact_tele','cover_image','intro','attention','club','mentor'];
        $formname = ['名称','地点','开始时间','价格','电话','封面','简介','宣传标语','承办俱乐部','负责人'];
        $formhead = collect($formname)->zip($formhead)->toArray();//压缩成数组
        $mentors = \Auth::user()->clubmodelforrole()->mentors->pluck('name','id');

        return view('admin.home.trainform',compact('pagename','mentors','tags','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/activityform.blade
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddTrainRequest $request)
    {
        //添加验证
        $data = array_merge(
            ['club_id'=>\Auth::user()->clubroleid(),
                'mentor_id'=>$request->input('mentor'),
            ],
            $request->all(),
            SoftImage::cover($request->file('cover_image'))//处理图片的函数
        );

        $train = Train::create($data);
//        $soft->tags()->attach($request->input('tags_list')); duoduiduo

        return redirect('admin/trains')->with('message', '成功添加'.$train->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        if (Gate::denies('club_Admin_train')) {
            return redirect('admin/trains');
        }
        $train = Train::findOrFail($id);
        $modelurl = 'trains';
        $pagename = $train->name;
        $formhead = ['name','address','activity_time','price','contact_tele','cover_image','intro','attention','mentor'];
        $formname = ['名称','地点','开始时间','价格','电话','封面','简介','宣传标语','教练'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        $mentors = \Auth::user()->clubmodelforrole()->mentors->pluck('name','id');
        $mentor_id = $train->mentor->id;
        return view('admin.home.edittrain',compact('pagename','train','mentors','mentor_id','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //添加验证
        $activity = Train::findOrFail($id);
        //查找
        $data = array_merge(
            $request->all(),
            ['mentor_id'=>$request->input('mentor')]
        );
        if($request->file('cover_image')){

        $data = array_merge($data, SoftImage::cover($request->file('cover_image')));
    }

        $activity->update($data);
        //更新
        return Success::back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        Train::destroy($id);
        return Success::back();
    }
}